define([] , function(){
	var controller = function( $scope , $timeout , $ajax , $element , $ctx ){
		var handler = {//树
			init 	: 	function(){
				var _this = this ;
				var nodes = [{
					name 	: 	'银基安全' ,
					open 	: 	false ,
					childType: 'orgData' ,
					nodeType: 'org' ,
					isParent : true 
				}] , setting = {
					callback 	: 	{
						onClick 	: 	function( event , treeId , node ){
							console.log( '组织点击事件' , node ) ;
							//_this.getOrgUser( node.dataId ) ;
						},
						onExpand 	: 	function( event , treeId , node ){
							if( node.children && node.children.length )
								return ;

							_this.childrenNodes( node , function( nodes ){
								_this.tree.addNodes( node , nodes ) ;
							});
						}
					}
				} ;
				//外部配置覆盖默认配置 .
				if( $scope.object && $scope.object.setting )
					$.extend( true , setting , $scope.object.setting ) ;

				this.tree = $.fn.zTree.init( $( $element ), setting , nodes );
				//对象引用到外部使用 .
				if( $scope.object ){
					$scope.object['tree'] = this.tree ;
				}
				return this ;
			},
			childrenNodes : 	function( parentNode , then ){
				var fncType = parentNode['childType'] ,
					parentId = parentNode['dataId'] ;
				if( typeof this[fncType] != 'function' )
					return ;
				var _this = this ;
				this[fncType]( parentId , function( nodes ){
					if( ( !nodes || !nodes.length ) &&  fncType == 'orgData' ){
						_this['deviceGroupData']( parentId , function( nodes ){
							then( nodes ) ;
						} , parentNode );
						return ;
					}
					then( nodes ) ;
				} , parentNode );
			},
			orgData: 	function( parentId , then ){
				$ajax.get( $ctx.api+'org?parentId=' + ( parentId || 0 ) ).success( function( result ){
					console.log( '加载组织信息' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;
					var nodes = [] ;
					if( result.data && result.data.length ){
						result.data.forEach( function( v , k ){
							nodes.push({
								name 	: 	v['shortName'] ,
								dataId 	: 	v['id'] ,
								childType: 	'orgData' ,
								nodeType :'org' ,
								extra 	: 	{
									orgId 	: 	v['id'] 
								} ,
								isParent: 	true
							});
						});
					}
					then( nodes ) ;
				});
			},
			deviceGroupData : 	function( orgId , then , node ){
				$ajax.get( $ctx.cfgApi+'deviceGroup' ).success( function( result ){
					console.log( '加载设备组信息' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;
					var nodes = [] ;
					if( result.data && result.data.length ){
						result.data.forEach( function( v , k ){
							nodes.push({
								name 	: 	v['name'] ,
								dataId 	: 	v['id'] ,
								nodeType :'deviceGroup' ,
								childType: 	'deviceData' ,
								extra 	: 	$.extend(true , {
									deviceGroupId 	: 	v['id'] 
								} , node['extra']  ),
								isParent: 	true
							});
						});
					}
					then( nodes ) ;
				});
			},
			deviceData 			: 	function( groupId , then , parentNode ){
				var extraData = parentNode['extra'] ;
				$ajax.get( $ctx.cfgApi+'device?_page=0&deviceGroup='+extraData['deviceGroupId']+'&userGroup='+extraData['orgId'] ).success( function( result ){
					console.log( '加载设备信息' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;
					var nodes = [] ;
					if( result.data && result.data.length ){
						result.data.forEach( function( v , k ){
							nodes.push({
								name 	: 	v['deviceName'] ,
								dataId 	: 	v['id'] ,
								childType: 	'deviceDimData' ,
								nodeType :'device' ,
								extra 	: 	$.extend(true , {
									deviceId 	: 	v['id'] 
								} , extraData ),
								isParent: 	true
							});
						});
					}
					then( nodes ) ;
				});
			},
			deviceDimData 		: 	function( deviceId , then , parentNode ){
				$ajax.get( $ctx.cfgApi+'device/'+deviceId+'/dim' ).success( function( result ){
					console.log( '加载设备维度信息' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;
					var nodes = [] , extraData = parentNode['extra'];
					if( result.data && result.data.length ){
						result.data.forEach( function( v , k ){
							nodes.push({
								name 	: 	v['dimName'] ,
								dataId 	: 	v['id'] ,
								childType: 	'deviceDimServiceData' ,
								nodeType :'deviceDim' ,
								extra 	: 	$.extend(true , {
									deviceDimId 	: 	v['id'] 
								} , extraData ),
								isParent: 	true
							});
						});
					}
					then( nodes ) ;
				});
			},
			deviceDimServiceData 	:  function( dimId , then , parentNode ){
				var extraData = parentNode['extra'] ;
				$ajax.get( $ctx.cfgApi+'/device/'+extraData['deviceId']+'/deviceService?dimId='+dimId ).success( function( result ){
					console.log( '加载设备维度信息' , result ) ;
					if( result.code != 0 )
						return alert( result.message ) ;
					var nodes = [] ;
					if( result.data && result.data.length ){
						result.data.forEach( function( v , k ){
							nodes.push({
								name 	: 	v['serviceName'] ,
								dataId 	: 	v['id'] ,
								childType: 	'deviceDimServiceData' ,
								nodeType : 'service' ,
								extra 	: 	$.extend(true , {
									deviceDimServiceId 	: 	v['id'] 
								} , extraData ),
								isParent: 	false
							});
						});
					}
					then( nodes ) ;
				});
			}
		}

		$timeout( function(){
			handler.init() ;
		});

	}
	app.register.directive('mdOrgDeviceTree' , function(){
		return {
			restrict 	: 	'E' ,
			replace 	: 	true ,
			scope 		: 	{
				object 	: 	'=object'
			} ,
			templateUrl : 	'assets/modules/md-org-device-tree/template.html' ,
			controller 	: 	controller 
		}
	});
})